package com.itheima.fruit;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class Test01 {

    /**
     * 榨汁机案例:
     *    通过配置文件上的信息.来创建不同的水果对象.去榨汁.
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        //1.读取配置文件
        //因为双亲委派模式，所以所有类的类加载器是一样的，都会调用最顶端的类加载器，故可用本来获取来加载器加载资源文件
        InputStream is = Test01.class.getClassLoader().getResourceAsStream("config.properties");
        Properties pro = new Properties();
        pro.load(is);

        String fruitName = pro.getProperty("fruitName");
        Class<?> clazz = Class.forName(fruitName);

        Constructor<?> constructor = clazz.getConstructor();
        Object fruit = constructor.newInstance();

        Field nameField = clazz.getDeclaredField("name");
        nameField.setAccessible(true);
        Object name = nameField.get(fruit);

        String count = pro.getProperty("count");
        System.out.println("请接收"+count+"杯"+name+"汁");

    }
}
